﻿Imports System.Windows.Forms

' 多國語言檔模組
Public Class clsMultiLang
    Public Structure stLangMenu
        Public Name As String
        Public FilePath As String
        Public DisplayName As String
    End Structure
    Public LangControlName() As String
    Public LangControlText() As String
    Public CurrentLang As Integer = 0
    Public LCID As Int32 = System.Globalization.CultureInfo.CurrentCulture.LCID
    Public LangFontName As String = "Arial"
    Public LangMenu() As stLangMenu
    Private LangMenuFile As String = Application.StartupPath & "\Lang\" & "LangMenu.txt"
    Private LangExporting As Boolean = False

    ' 載入多國語言清單
    Public Sub LoadLangMenu()
        Dim LangCnt As Integer = 1
        Dim DefLang As String = "English"

        ReDim LangMenu(0)
        LangMenu(0).Name = "1033"
        LangMenu(0).DisplayName = "English"
        LangMenu(0).FilePath = Application.StartupPath & "\Lang\English.lang"

        If My.Computer.FileSystem.FileExists(LangMenuFile) Then
            Try
                Dim tStr() As String = Split(My.Computer.FileSystem.ReadAllText(LangMenuFile), vbCrLf)
                If IsNothing(tStr) = False Then
                    For i As Integer = 0 To tStr.Length - 1
                        If tStr(i) <> "" AndAlso InStr(tStr(i), "=") > 0 Then
                            Dim eachStr() As String = Split(tStr(i), "=")
                            If UCase(eachStr(0)) = "DEFAULT" Then
                                DefLang = eachStr(1)
                            ElseIf UCase(eachStr(0)) <> "ENGLISH" AndAlso UCase(eachStr(0)) <> "1033,ENGLISH" Then
                                If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Lang\" & eachStr(1)) Then
                                    ReDim Preserve LangMenu(LangCnt)
                                    Dim nameStr() As String = Split(eachStr(0), ",")
                                    LangMenu(LangCnt).Name = nameStr(0) 'eachStr(0)
                                    If nameStr.Length > 1 Then
                                        LangMenu(LangCnt).DisplayName = nameStr(1)
                                    Else
                                        LangMenu(LangCnt).DisplayName = nameStr(0)
                                    End If
                                    LangMenu(LangCnt).FilePath = Application.StartupPath & "\Lang\" & eachStr(1)
                                    LangCnt += 1
                                End If
                            End If
                        End If
                    Next
                End If
            Catch ex As Exception
            Finally
                ' 如果第1次執行, 使用系統預設語系
                If DefLang.ToUpper(System.Globalization.CultureInfo.InvariantCulture) = "AUTO" Then DefLang = LCID.ToString.Trim
                For i As Integer = 0 To LangMenu.Length - 1
                    If LangMenu(i).Name = DefLang Then
                        CurrentLang = i
                        Exit For
                    End If
                Next
            End Try
        End If
    End Sub
    ' 儲存目前選擇語言
    Public Sub SaveLangMenu()
        If My.Computer.FileSystem.FileExists(LangMenuFile) Then
            Try
                Dim tStr() As String = Split(My.Computer.FileSystem.ReadAllText(LangMenuFile), vbCrLf)
                If IsNothing(tStr) = False Then
                    Dim i As Integer = 0
                    For i = 0 To tStr.Length - 1
                        If InStr(tStr(i), "Default=", CompareMethod.Text) > 0 Then Exit For
                    Next
                    If i >= tStr.Length Then
                        ReDim Preserve tStr(i)
                    End If
                    tStr(i) = "Default=" & LangMenu(CurrentLang).Name
                    My.Computer.FileSystem.WriteAllText(LangMenuFile, Join(tStr, vbCrLf), False)
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub
    ' 載入目前選擇語言
    Public Function LoadLangFile() As Boolean
        Erase LangControlName
        Erase LangControlText
        Dim tFileName As String = LangMenu(CurrentLang).FilePath
        Dim tReadCnt As Integer = 0
        Dim tFormCount As Integer = 0
        If CurrentLang = 0 Then LangFontName = "Arial"

        If Dir(tFileName) <> "" Then
            If FileLen(tFileName) > 2 Then
                'Dim tFileNo As Integer = FreeFile()
                Dim tStr As String = ""
                Dim tSplitStr() As String
                Dim i As Integer
                ' 以Unicode方式讀取多國語言檔
                tStr = My.Computer.FileSystem.ReadAllText(tFileName, System.Text.Encoding.Unicode)
                tSplitStr = Split(tStr, Chr(&HD) & Chr(&HA))
                If tSplitStr.Length > 0 Then
                    Erase LangControlName
                    Erase LangControlText
                    For i = 0 To UBound(tSplitStr)
                        If tSplitStr(i) <> "" Then
                            If InStr(tSplitStr(i), "=") > 0 Then
                                If UCase(Split(tSplitStr(i), "=")(0)) = "FONTNAME" Then
                                    LangFontName = Split(tSplitStr(i), "=")(1)
                                Else
                                    ReDim Preserve LangControlName(tReadCnt)
                                    ReDim Preserve LangControlText(tReadCnt)
                                    LangControlName(tReadCnt) = Trim(Split(tSplitStr(i), "=")(0))
                                    LangControlText(tReadCnt) = Split(tSplitStr(i), "=")(1)
                                    tReadCnt += 1
                                End If
                            End If
                        End If
                    Next
                End If
            End If
        End If
    End Function
    ' 以Unicode方式寫入多國語言檔
    Public Sub WriteControlText(ByVal InControl As Windows.Forms.Control, ByVal tFileName As String, ByVal pName As String) ', ByVal tFileNo As Integer)
        Try
            If InControl.Controls.Count > 0 Then
                Dim tCtrl As Control
                For Each tCtrl In InControl.Controls
                    WriteControlText(tCtrl, tFileName, pName) '& tCtrl.Name)
                Next
                Try
                    If InControl.Text <> "" Then
                        Dim pText As String = pName & "->" & InControl.Name
                        Dim tStr = pText & Strings.StrDup(IIf(pText.Length < 50, 50 - pText.Length, 0), " ") & "=" & InControl.Text & vbCrLf
                        ' 以Unicode方式寫入多國語言檔
                        My.Computer.FileSystem.WriteAllText(tFileName, tStr, True, System.Text.Encoding.Unicode)
                    End If
                Catch ex As Exception

                End Try

            Else
                Try
                    If InControl.Text <> "" Then
                        Dim pText As String = pName & "->" & InControl.Name
                        Dim tStr = pText & Strings.StrDup(IIf(pText.Length < 50, 50 - pText.Length, 0), " ") & "=" & InControl.Text & vbCrLf
                        ' 以Unicode方式寫入多國語言檔
                        My.Computer.FileSystem.WriteAllText(tFileName, tStr, True, System.Text.Encoding.Unicode)
                    End If

                Catch ex As Exception

                End Try
            End If
        Catch ex As Exception
            Try
                If InControl.Text <> "" Then
                    Dim pText As String = pName & "->" & InControl.Name
                    Dim tStr = pText & Strings.StrDup(IIf(pText.Length < 50, 50 - pText.Length, 0), " ") & "=" & InControl.Text & vbCrLf
                    ' 以Unicode方式寫入多國語言檔
                    My.Computer.FileSystem.WriteAllText(tFileName, tStr, True, System.Text.Encoding.Unicode)
                End If
            Catch tex As Exception

            End Try
        End Try

    End Sub
    ' 載入表單物件語言
    Public Sub LoadControlText(ByVal InControl As Control, ByVal pName As String)
        Try
            If InControl.Controls.Count > 0 Then
                Dim tCtrl As Control
                For Each tCtrl In InControl.Controls
                    LoadControlText(tCtrl, pName)
                Next
                Try
                    Dim tIndex As Integer = Array.IndexOf(LangControlName, pName & "->" & InControl.Name)
                    If tIndex >= 0 Then
                        InControl.Text = LangControlText(tIndex)
                    End If
                    InControl.Font = New System.Drawing.Font(LangFontName, InControl.Font.Size, InControl.Font.Style)

                Catch ex As Exception
                End Try
            Else
                Try
                    Dim tIndex As Integer = Array.IndexOf(LangControlName, pName & "->" & InControl.Name)
                    If tIndex >= 0 Then
                        InControl.Text = LangControlText(tIndex)
                    End If
                    InControl.Font = New System.Drawing.Font(LangFontName, InControl.Font.Size, InControl.Font.Style)

                Catch ex As Exception
                End Try
            End If
        Catch ex As Exception
            Try
                Dim tIndex As Integer = Array.IndexOf(LangControlName, pName & "->" & InControl.Name)
                If tIndex >= 0 Then
                    InControl.Text = LangControlText(tIndex)
                End If
                InControl.Font = New System.Drawing.Font(LangFontName, InControl.Font.Size, InControl.Font.Style)

            Catch tex As Exception
            End Try
        End Try
    End Sub
    ' 載入表單語言
    Public Sub LoadLang(ByRef tForm As Form)
        If LangExporting Then Exit Sub
        If IsNothing(LangControlName) Then Exit Sub
        If LangControlName.Length = 0 Then Exit Sub
        LoadControlText(tForm, tForm.Name)
    End Sub
    ' 製作多國語言檔
    Public Sub GenerateLangFile(ByRef FormsArray() As Windows.Forms.Form)
        Dim tFilename As String = Application.StartupPath & "\Lang\Lang.txt"
        My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.GetParentPath(tFilename))
        If Dir(tFilename) <> "" Then Kill(tFilename)

        For i As Integer = 0 To FormsArray.Length - 1
            WriteControlText(FormsArray(i), tFilename, FormsArray(i).Name)
        Next
    End Sub
    ' 製作多國語言檔表單
    Public Sub ExportLangFile(ByVal al_frm As List(Of Form))
        'Dim al_frm As New List(Of Form)
        'With al_frm
        '    .Add(My.Forms.frmAlarmLog)
        '    .Add(My.Forms.frmMain)
        '    .Add(My.Forms.frmReStart)
        '    .Add(My.Forms.frmSetup)
        'End With

        Dim tForms() As Form = al_frm.ToArray
        LangExporting = True
        GenerateLangFile(tForms)
        LangExporting = False
    End Sub
End Class
